<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-30 15:52:56
 * @LastEditTime: 2023-11-30 15:58:47
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    :title='currentTitle'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    v-bind='canEdit ? {} : { footer: null}'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-form-item label='主键' v-show='false'>
          <a-input
            v-decorator="['id']"></a-input>
        </a-form-item>

        <a-form-item label='算法名称'>
          <a-input v-show='canEdit'
                   v-decorator="['algorithmName',{ rules: [
                     { required: true, message: '请输入算法名称!' },
             { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}] }]"></a-input>
          <div v-show='!canEdit' class='view-item'>{{ model && model.algorithmName }}</div>
        </a-form-item>

        <a-form-item label='算法地址'>
          <a-input v-show='canEdit'
                   v-decorator="['algorithmAddress', { rules: [{ required: true, message: '请输入算法地址!' }] }]"></a-input>
          <div v-show='!canEdit' class='view-item'>{{ model && model.algorithmAddress }}</div>
        </a-form-item>

        <a-form-item label='算法入参'>
          <a-input v-show='canEdit' v-decorator="[
              'algorithmParam'
            ]"></a-input>
          <div v-show='!canEdit' class='view-item'>{{ model && model.algorithmParam }}</div>
        </a-form-item>

        <a-form-item label='调用方式'>
          <a-select v-show='canEdit' v-decorator="[
              'method',
              { rules: [{ required: true, message: '请选择调用方式!' }] },
            ]" placeholder='请选择调用方式' :options='methodList'>
          </a-select>
          <div v-show='!canEdit' class='view-item'>
            {{ model && methodList.find(item => item.key === model.method)['label'] }}
          </div>
        </a-form-item>

        <a-form-item label='版本号'>
          <a-input v-show='canEdit' v-decorator="[
              'versionNo',
              { rules: [{ required: true, message: '请输入版本号!' }] },
            ]"></a-input>
          <div v-show='!canEdit' class='view-item'>{{ model && model.versionNo }}</div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import { namePattern } from '@/reges'
import pick from 'lodash.pick'

// 表单字段
const fields = ['id', 'algorithmName', 'algorithmAddress', 'algorithmParam', 'method', 'versionNo']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    canEdit: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    currentTitle() {
      if (!this.canEdit) {
        return '算法配置详情'
      }
      return (this.model ? '编辑' : '新增') + '算法配置'
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      namePattern,
      form: this.$form.createForm(this),
      methodList: [
        { key: 'get', label: 'GET' },
        { key: 'post', label: 'POST' }
      ]
    }
  },
  created() {
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
